package com.jd.sunny.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.jd.sunny.exception.DateUtilException;
import org.apache.log4j.Logger;

/**
 * @author :孙林
 * @ClassName:DateUtil
 * @Description: 时间工具类(完成)
 * @date :2012-8-22下午2:46:31
 */

public class DateUtil {

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        long millisecond = poor(new Date(), new Date());
        Logger logger = LogUtil.getLogger();
        logger.info("时间差为 " + millisecond + "毫秒");

        boolean b = DateUtil.isDate("1990-10-55", "yyyy-MM-dd");
        System.out.println(b);

    }

    /**
     * 获取当前日期和时间
     *
     * @return Date类型
     */
    public static Date getCurrentDateAndTime() {
        return Calendar.getInstance().getTime();
    }

    /**
     * 获取当前时间的格式化字符串，yyyy-MM-dd
     *
     * @return
     */
    public static String getCurrentDateString() {
        return new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
    }

    /**
     * 获取当前时间的格式化字符串，yyyyMMdd
     *
     * @return
     */
    public static String getCurrentDateStringWithPattern(String pattern) {
        if (null == pattern || pattern.length() <= 0) {
            return "";
        }
        return new SimpleDateFormat(pattern).format(Calendar.getInstance().getTime());
    }

    /**
     * 获取当前时间的格式化字符串，HH:mm:ss
     *
     * @return
     */
    public static String getCurrentTimeString() {
        return new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
    }

    /**
     * 获取当前时间的格式化字符串，yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getCurrentDateAndTimeString() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
    }

    /**
     * 获取日期格式化对象
     *
     * @param pattern
     * @return
     */
    public SimpleDateFormat getFormat(String pattern) {
        if (null == pattern || pattern.length() <= 0) {
            return null;
        }
        return new SimpleDateFormat(pattern);
    }

    /**
     * 将字符串格式化成日期
     *
     * @param dateString
     * @param pattern
     * @return
     * @throws DateUtilException
     */
    public static Date parse(String dateString, String pattern) throws DateUtilException {
        if (null == dateString || dateString.length() <= 0 || null == pattern || pattern.length() <= 0) {
            return null;
        }
        try {
            return new SimpleDateFormat(pattern).parse(dateString);
        } catch (ParseException e) {
            throw DateUtilException.DateUtilException_PARSE;
        }
    }

    /**
     * 测试给定的字符串是否是合法的日期格式，不能判断日期是否正确,例如：1990-88-88会返回true
     * @param dateString
     * @param pattern
     * @return
     */
    public static boolean isDateFormat(String dateString, String pattern) {
        if (dateString == null || dateString.length() < 0 || pattern == null || pattern.length() <= 0) {
            return false;
        }
        try {
            new SimpleDateFormat(pattern).parse(dateString);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 测试给定的字符串是否是合法的日期格式，不能判断日期是否正确
     *
     * @param dateString
     * @param pattern
     * @return
     * @throws DateUtilException
     */
    public static boolean isDate(String dateString, String pattern) {
        if (dateString == null || dateString.length() < 0 || pattern == null || pattern.length() <= 0) {
            return false;
        }
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            //设置解析过程严格
            simpleDateFormat.setLenient(false);
            simpleDateFormat.parse(dateString);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 求两个时间的毫秒值的差，第一个减去第二个为结果
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long poor(Date date1, Date date2) {
        if (null == date1 || null == date2) {
            return 0;
        }
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(date1);
        calendar2.setTime(date2);
        long millisecond = calendar1.getTimeInMillis() - calendar2.getTimeInMillis();
        return millisecond;
    }

}
